/*
Copyright (c) 2007-2009, Yusuke Yamamoto
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
 * Neither the name of the Yusuke Yamamoto nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package kdweibo4j;

import java.util.ArrayList;
import java.util.List;

import kdweibo4j.http.Response;
import kdweibo4j.org.json.JSONArray;
import kdweibo4j.org.json.JSONException;
import kdweibo4j.org.json.JSONObject;


/**
 * 群组信息
 * 
 * @author winters_huang
 */
public class Group extends WeiboResponse implements java.io.Serializable {

	private String id = "";
	private String name = "";
	private String profile_image_url = "";
	private String description = "";
	private String bulletin = "";
	private String type = "";
	private Detail detail = null; 
	private long newMsgCnt = 0;
	

	public Group() {

	}

	Group(Response res) throws WeiboException {
		super(res);
		JSONObject json = res.asJSONObject();
		constructJson(json);
	}

	public Group(JSONObject json) throws WeiboException, JSONException {
		constructJson(json);
	}

	public Group(String str) throws WeiboException, JSONException {
		// StatusStream uses this constructor
		super();
		JSONObject json = new JSONObject(str);
		constructJson(json);
	}
	
	/*
	 * 变量存储
	 */
	public void setId(String id){
		this.id = id;
	}
	
	public String getId(){
		return this.id;
	}
	
	public void setName(String name){
		this.name = name;
	}
	
	public String getName(){
		return this.name;
	}
	
	public void setProfile_image_url(String profile_image_url){
		this.profile_image_url = profile_image_url;
	}
	
	public String getProfile_image_url(){
		return this.profile_image_url;
	}
	
	public void setDescription(String description){
		this.description = description;
	}
	
	public String getDescription(){
		return this.description;
	}
	
	public void setBulletin(String bulletin){
		this.bulletin = bulletin;
	}
	
	public String getBulletin(){
		return this.bulletin;
	}
	
	public void setDetail(Detail detail){
		this.detail = detail;
	}
	
	public Detail getDetail(){
		return this.detail;
	}
	
	public String setType(String type){
		return this.type;
	}
	
//	public void setNewMsgCnt(long newMsgCnt){
//		this.newMsgCnt = newMsgCnt;
//	}
//	
//	public long getNewMsgCnt(){
//		return this.newMsgCnt;
//	}
	
	/* modify by Reilost add some field */
	private void constructJson(JSONObject json) throws WeiboException {
		try {
			id = json.getString("id");
			name = json.getString("name");
			profile_image_url = json.getString("profile_image_url");
			description = json.getString("description");
			bulletin = json.getString("bulletin");
			type = json.getString("type");
			detail = new Detail(json.getJSONObject("detail"));
			
		} catch (JSONException je) {
			throw new WeiboException(je.getMessage() + ":" + json.toString(),
					je);
		}
	}

	static List<Group> constructGroups(Response res) throws WeiboException {
		try {
			JSONArray list = res.asJSONArray();
			int size = list.length();
			List<Group> groups = new ArrayList<Group>(size);

			for (int i = 0; i < size; i++) {
				groups.add(new Group(list.getJSONObject(i)));
			}
			return groups;
		} catch (JSONException jsone) {
			throw new WeiboException(jsone);
		} catch (WeiboException te) {
			throw te;
		}

	}
	
	@Override
	public int hashCode() {
		return (int) id.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (null == obj) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		return obj instanceof Group && ((Group) obj).id == this.id;
	}

	@Override
	public String toString() {
		return "";
	}
	
	/**
	 * 群组明细
	 * 
	 * @author winters_huang
	 *
	 */
	class Detail implements java.io.Serializable {
		private boolean needAudit = false;
		private User creator = null;
		private long createTime;
		private long updateTime;
		private int messageCount;
		private int memberCount;
		private int docmentCount;
		
		Detail(JSONObject json) throws WeiboException {
			super();
			init(json);
		}
		/*
		 * 变量存储
		 */
		public void setNeedAudit(boolean needAudit){
			this.needAudit = needAudit;
		}
		
		public boolean getNeedAudit(){
			return this.needAudit;
		}
		
		public void setCreator(User creator){
			this.creator = creator;
		}
		
		public User getCreator(){
			return this.creator;
		}
		
		public void setCreateTime(long createTime){
			this.createTime = createTime;
		}
		
		public long getCreateTime(){
			return this.createTime;
		}
		
		public void setUpdateTime(long updateTime){
			this.updateTime = updateTime;
		}
		
		public long getUpdateTime(){
			return this.updateTime;
		}
		
		public void setMessageCount(int messageCount){
			this.messageCount = messageCount;
		}
		
		public int getMessageCount(){
			return this.messageCount;
		}
		
		public void setMemberCount(int memberCount){
			this.memberCount = memberCount;
		}
		
		public int getMemberCount(){
			return this.memberCount;
		}
		
		public void setDocmentCount(int docmentCount){
			this.docmentCount = docmentCount;
		}
		
		public int getDocmentCount(){
			return this.docmentCount;
		}
		
		
		/**
		 * 传入json对象，实例化detail
		 * @param json
		 * @throws WeiboException
		 */
		private void init(JSONObject json) throws WeiboException {
			if (json != null) {
				try {
					needAudit = json.getBoolean("needAudit");
					creator = new User(json.getJSONObject("creator"));
					createTime = json.getLong("createTime");
					updateTime = json.getLong("updateTime");
					messageCount = json.getInt("messageCount");
					memberCount = json.getInt("memberCount");
					docmentCount = json.getInt("docmentCount");
				} catch (JSONException jsone) {
					throw new WeiboException(jsone.getMessage() + ":"
							+ json.toString(), jsone);
				}
			}
		}
	}
}
